<?php

namespace LowCode\Traits;

trait HasDirective
{
    /**
     * 组件 v-if
     * @param string $value
     * @param string|null $id
     * @param array $options
     * @return $this
     */
    public function vIf(string $value, string $id = null, array $options = []): static
    {
        $options['value'] = jsExp($value, $id);
        return $this->directive("vIf", $options);
    }

    /**
     * 组件 v-show
     * @param string $value
     * @param string|null $id
     * @param array $options
     * @return $this
     */
    public function vShow(string $value, string $id = null, array $options = []): static
    {
        $options['value'] = jsExp($value, $id);
        return $this->directive("vShow", $options);
    }

    /**
     * 组件 v-bind
     * @param string $prop
     * @param string $value
     * @param string|null $id
     * @return $this
     */
    public function vBind(string $prop, string $value, string $id = null): static
    {
        return $this->prop($prop, jsExp($value, $id));
    }

    /**
     * 组件 v-model
     * @param string $value
     * @param string|null $id
     * @param array $options
     * @return $this
     */
    public function vModel(string $value, string $id = null, array $options = []): static
    {
        return $this->vModelArg("modelValue", $value, $id, $options);
    }

    /**
     * 组件 v-model:arg
     * @param string|array $name
     * @param string $value
     * @param string|null $id
     * @param array $options
     * @return $this
     */
    public function vModelArg(string|array $name = "modelValue", string $value = "", string $id = null, array $options = []): static
    {
        $options['value'] = jsExp($value, $id);
        $options['arg'] = $options['arg']??$name;
        return $this->directive("vModel", $options);
    }

    /**
     * 组件 v-for
     * @param string $value
     * @param string $item
     * @param string $index
     * @param array $options
     * @param string|null $id
     * @return $this
     */
    public function vFor(string $value, string $item = "item", string $index = "index", array $options = [], string $id = null): static
    {
        $options['value'] = jsExp($value, $id);
        $options['iterator'] = [
            "item"=>$item,
            "index"=>$index,
        ];
        return $this->directive("vFor", $options);
    }

    /**
     * @param string $name
     * @param array $options
     * @return $this
     */
    public function directive(string $name, array $options = []): static
    {
        $options['name'] = $name;
        $directives = $this->get("directives",[]);
        $directives[] = $options;
        return $this->directives($directives);
    }
}
